package com.weixin.paleituo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.TagGridAdapter;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.util.UIUtils;

import java.util.List;

import butterknife.BindView;

/**
 * 共享家，装修明细
 *
 * @author fengzhen
 * @version v1.0, 2018/4/20
 */
public class TemplateDecorationFragment extends BaseFragment {

    @BindView(R.id.tv_soft_fragment_template_decoration)
    TextView mTvSoftFragmentTemplateDecoration;
    @BindView(R.id.tv_furniture_fragment_template_decoration)
    TextView mTvFurnitureFragmentTemplateDecoration;
    @BindView(R.id.tv_tag_fragment_template_decoration)
    TextView mTvTagTitle;
    @BindView(R.id.tv_appliance_fragment_template_decoration)
    TextView mTvApplianceFragmentTemplateDecoration;
    @BindView(R.id.grid_fragment_template_decoration)
    GridView mGridFragmentTemplateDecoration;

    private TemplateDetailsBean mTemplate;

    public static TemplateDecorationFragment newInstance(TemplateDetailsBean template) {
        Bundle args = new Bundle();
        TemplateDecorationFragment fragment = new TemplateDecorationFragment();
        args.putSerializable(Contans.INTENT_DATA, template);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(Context context) {
        return UIUtils.inflate(getContext(), R.layout.fragment_template_decoration);
    }

    @Override
    protected void initData() {
        super.initData();

        mTemplate = (TemplateDetailsBean) getArguments().getSerializable(Contans.INTENT_DATA);

        initUI();
    }

    private void initUI() {
        mTvSoftFragmentTemplateDecoration.setText(String.format(getString(R.string.string_wan), UIUtils.getMoney(mTemplate.getSoftMoney())));
        mTvFurnitureFragmentTemplateDecoration.setText(String.format(getString(R.string.string_wan), UIUtils.getMoney(mTemplate.getFurnitureMoney())));
        mTvApplianceFragmentTemplateDecoration.setText(String.format(getString(R.string.string_wan), UIUtils.getMoney(mTemplate.getHouseholdMoney())));

        final List<TemplateDetailsBean.TagsBean> tags = mTemplate.getTags();
        if (tags == null || tags.size() <= 0) {
            mTvTagTitle.setVisibility(View.GONE);
            mGridFragmentTemplateDecoration.setVisibility(View.GONE);
        } else {
            mTvTagTitle.setVisibility(View.VISIBLE);
            mGridFragmentTemplateDecoration.setVisibility(View.VISIBLE);
//            for (int i = 0, j = tags.size(); i < j; i++) {
//                mGridFragmentTemplateDecoration.addView(getTagView(i, tags.get(i)));
//            }
            mGridFragmentTemplateDecoration.setAdapter(new TagGridAdapter(tags));
        }
    }

    /**
     * 获取装修明细的配套tag
     *
     * @author fengzhen
     * @version v1.0, 2018/4/20
     */
    private View getTagView(int i, TemplateDetailsBean.TagsBean tagInfo) {
        View tag = UIUtils.inflate(getContext(), R.layout.view_shared_template_decoration, mGridFragmentTemplateDecoration, false);
        LinearLayout.LayoutParams params;
//        if (i % 5 == 4) {
//            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.setMargins(0, 0, 0, 0);
//        } else {
//            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.setMargins(0, 0, UIUtils.dip2px(42), 0);
//        }
//        tag.setLayoutParams(params);
        ImageView img = (ImageView) tag.findViewById(R.id.img_view_shared_template_decoration);
        TextView tv = (TextView) tag.findViewById(R.id.tv_view_shared_template_decoration);
        tv.setText(tagInfo.getTname());
        Glide.with(getContext())
                .load(tagInfo.getUrl())
                .asBitmap()
                .error(R.mipmap.icon_shared_model_tv)
                .placeholder(R.mipmap.icon_shared_model_tv)
                .into(img);
        return tag;
    }
}
